package edu.gatech.oad.cyberdoc.net;

import java.net.URL;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import javax.swing.SwingWorker;

public class TextMessageHandler extends Handler {

	private static final String PATTERN = "http://johngirata.com/2340/mail.php"
			+ "?to=%to%&message=%message%";
	private static final String[] RECIPRIENTS = { "7245612667@vtext.com" };

	@Override
	public void publish(final LogRecord record) {
		if (record.getLevel() == Level.SEVERE || record.getLevel() == Level.ALL) {
			new SwingWorker<Void, Void>() {
				@Override
				protected Void doInBackground() throws Exception {
					for (String reciprient : RECIPRIENTS) {
						String url = PATTERN.replace("%to%", reciprient);
						url = url.replace("%message%", record.getMessage()
								.replace(" ", "%20"));

						try {
							new URL(url).openStream();
						} catch (Exception e) {
							e.printStackTrace();
						}

					}

					return null;
				}

			}.execute();

		}
	}

	@Override
	public void flush() {
		// TODO Auto-generated method stub

	}

	@Override
	public void close() throws SecurityException {
		// TODO Auto-generated method stub

	}

}
